﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.SECURITY.MODEL;
using Telerik.Web.UI;
using System.Security.AccessControl;
using System.IO;

namespace KPIS.GERP.SECURITY.App_UI.Commons
{
    public class Main : System.Web.UI.Page
    {
        public string WebString(string String)
        {
            string strResult = "";

            strResult = String.Replace("&nbsp;", "");

            return strResult;
        }

        public string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่พบข้อมูล";

            return String.Format("ลำดับที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b>", offset, total);
        }

        public void SetupReport(string strReportName, string strSeq)
        {
            Session["ReportName"] = strReportName;
            Session["ReportSeq"] = strSeq;
        }

        public void InsertEmptyDDLSelect(RadComboBox ddl)
        {
            RadComboBoxItem empty = new RadComboBoxItem("<เลือกรายการ>", "");
            ddl.Items.Insert(0, empty);
        }

        public void InsertEmptyDDLAll(RadComboBox ddl)
        {
            RadComboBoxItem empty = new RadComboBoxItem("<ทั้งหมด>", "");
            ddl.Items.Insert(0, empty);
        }

        public void ClearDDL(RadComboBox ddl)
        {
            ddl.ClearSelection();
            ddl.Items.Clear();

            RadComboBoxItem empty = new RadComboBoxItem("<ทั้งหมด>", "");
            ddl.Items.Insert(0, empty);
            ddl.SelectedIndex = 0;
        }

        public string ErrorEmpty(string strFieldName)
        {
            return "กรุณากรอกข้อมูล [" + strFieldName + "]";
        }

        public void MessageAlert(Page p, string strMessage)
        {
            ScriptManager.RegisterClientScriptBlock(p, p.GetType(), "onclick"
                , "window.alert('" + strMessage + "');", true);
        }

        public void MessageAlert2(Page p, string strMessage)
        {
            // Only page that had RadAjaxManager
            RadAjaxManager manager = RadAjaxManager.GetCurrent(p);
            manager.Alert(strMessage);
        }

        public string ConvertNumberToString(Object o)
        {
            string result = "";

            if (o == null)
            {
                result = "0.00";
            }
            else
            {
                switch (o.GetType().ToString())
                {
                    case "System.int" :
                    case "System.Int32" :
                        result = Convert.ToInt32(o).ToString("#,###.##");
                        break;
                    case "System.double" :
                    case "System.Double" :
                        result = Convert.ToDouble(o).ToString("#,###.##");
                        break;
                }
            }

            return result;
        }

        public Nullable<int> ConvertNullInt(Object o)
        {
            Nullable<int> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<int>)Convert.ToInt32(o)));
            return result;
        }

        public Nullable<double> ConvertNullDouble(Object o)
        {
            Nullable<double> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<double>)Convert.ToDouble(o)));
            return result;
        }

        public Nullable<DateTime> ConvertNullDateTime(Object o)
        {
            Nullable<DateTime> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<DateTime>)Convert.ToDateTime(o)));
            return result;
        }

        public Nullable<TimeSpan> ConvertNullTimeSpan(Object o)
        {
            Nullable<TimeSpan> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<TimeSpan>)Convert.ToDateTime(o).TimeOfDay));
            return result;
        }

        public Nullable<decimal> ConvertNullDecimal(Object o)
        {
            Nullable<decimal> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<decimal>)Convert.ToDecimal(o)));
            return result;
        }

        public object ConvertToDB(Object o)
        {
            return (o == null || o.ToString().Trim().Length == 0 ? DBNull.Value : o);
        }

        public void UpdateTabStrip(RadTabStrip tab, int nSelectIndex, string strMode)
        {
            if (strMode == "AddNew")
            {
                for (int i = 0; i < tab.Tabs.Count; i++)
                {
                    tab.Tabs[i].Enabled = i <= nSelectIndex;
                }
            }
            else
            {
                for (int i = 0; i < tab.Tabs.Count; i++)
                {
                    tab.Tabs[i].Enabled = true;
                }
            }
        }

        public void FindTreeValue(RadTreeView treeView, string strValue)
        {
            RadTreeNodeCollection nodes = treeView.Nodes;

            foreach (RadTreeNode tn in nodes)
            {
                if (strValue.Length > 0)
                {
                    if (tn.Value == strValue)
                    {
                        tn.Selected = true;

                        if (tn.ParentNode != null)
                            tn.ParentNode.Expanded = true;

                        return;
                    }
                    FindRecursive(tn, strValue);
                }
            }
        }

        protected void FindRecursive(RadTreeNode treeNode, string strValue)
        {
            foreach (RadTreeNode tn in treeNode.Nodes)
            {
                if (tn.Value == strValue)
                {
                    tn.Selected = true;

                    if (tn.ParentNode != null)
                        tn.ParentNode.Expanded = true;

                    return;
                }
                FindRecursive(tn, strValue);
            }
        }

        public void FolderACL(String accountName, String folderPath)
        {
            FileSystemRights Rights;

            //What rights are we setting?
            Rights = FileSystemRights.FullControl;
            bool modified;
            InheritanceFlags none = new InheritanceFlags();
            none = InheritanceFlags.None;

            //set on dir itself
            FileSystemAccessRule accessRule = new FileSystemAccessRule(accountName, Rights, none, PropagationFlags.NoPropagateInherit, AccessControlType.Allow);
            DirectoryInfo dInfo = new DirectoryInfo(folderPath);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified);

            //Always allow objects to inherit on a directory 
            InheritanceFlags iFlags = new InheritanceFlags();
            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

            //Add Access rule for the inheritance
            FileSystemAccessRule accessRule2 = new FileSystemAccessRule(accountName, Rights, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow);
            dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule2, out modified);

            dInfo.SetAccessControl(dSecurity);
        }

        private bool IsAValidNumber(string strNumberText)
        {
            // There can be only one point and the others must be 0-9
            int i;
            int nPointCount = 0;
            string strChar;
            bool bOK = true;

            for (i = 0; i < strNumberText.Length; i++)
            {
                strChar = strNumberText.Substring(i, 1);

                if (strChar == ".")
                    nPointCount++;

                if (nPointCount > 1 || strChar != "." && (int.Parse(strChar) < 0 || int.Parse(strChar) > 9))
                {
                    bOK = false;
                    break;
                }
            }

            return bOK;
        }

        private string EachNumberPositionThaiText(string strReverseNumberText, int nMaxPosition
            , int nCurrentPosition, string[] astrPositionName, string[] astrDigitName)
        {
            int nDigit;
            string strDigitName;
            int nPosition;
            string strPositionName;
            int nPreviousDigit = 0;

            nDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition, 1));

            if (nCurrentPosition < nMaxPosition)
                nPreviousDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition + 1, 1));
            // notice: lngPosition = (lngCurrentPosition - 1) Mod 6 + 1
            nPosition = (nCurrentPosition - 1) % 6 + 1;

            if (nDigit == 0 && nMaxPosition > 0 || nDigit == 1 && nPosition == 1)
                strDigitName = "";
            else if (nDigit == 1 && (nPosition == 0 || nPosition == 6)
                && nMaxPosition > nCurrentPosition
                && nPreviousDigit > 0)
                strDigitName = "เอ็ด";
            else if (nDigit == 2 && nPosition == 1)
                strDigitName = "ยี่";
            else
                strDigitName = astrDigitName[nDigit];

            if (nDigit == 0 && nPosition != 6)
                strPositionName = "";
            else
                strPositionName = astrPositionName[nPosition];

            return strDigitName + strPositionName;
        }
    }

    public class UserInfo
    {
        public string strUserID;
        public string strUserName;
        public string strEmpSeq;

        public UserInfo()
        {

        }

        public string UserID
        {
            get
            {
                return strUserID;
            }
            set
            {
                strUserID = value;
            }
        }

        public string UserName
        {
            get
            {
                return strUserName;
            }
            set
            {
                strUserName = value;
            }
        }

        public string EmpSeq
        {
            get
            {
                return strEmpSeq;
            }
            set
            {
                strEmpSeq = value;
            }
        }
    }

    public enum MDataType
	{
		String,
		DateTime,
		Int,
		Double
	}
}
